use actix_cors::Cors;
use actix_files as files;
use actix_web::{middleware, web, App, HttpServer};

mod http;
mod udp_server;
mod websocket_server;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "debug");
    env_logger::init();

    let http_server = HttpServer::new(|| {
        let cors = Cors::default()
            // .allowed_origin("http://127.0.0.1")
            // .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
            .allow_any_origin()
            // .allow_any_header()
            .allow_any_method();
        App::new()
            .wrap(cors)
            .wrap(middleware::Logger::default())
            .route("/ws", web::get().to(websocket_server::ws_index))
            .service(http::video)
            // static files
            .service(files::Files::new("/", "static/").index_file("index.html"))
    });
    actix_rt::spawn(udp_server::start());
    let http_server = http_server.bind("127.0.0.1:9001")?;
    let http_task = http_server.run();
    // try_join!(udp_task, http_task);
    // Ok(())
    http_task.await
}
